package edu.nyu.hps.architecture;

import java.util.Random;

public class Mouse extends Player {

	int opponentX, opponentY; 
	public Mouse(int x, int y) {
		super();
		this.x = x;
		this.y = y;
		this.who = "mouse";
		this.time = 120;
		this.ready = false;
		this.totalMoves = 0;
		this.caught = false;
	}


	@Override
	public boolean validMove(String[] move) {

		// One extra because move[0] holds "MOVE"
		if (move.length > 2) {
			return false;
		}

		// I am hardcoding this because mouse can move just one step.
		String[] coordinates = move[1].split(",");
		int x = Integer.parseInt(coordinates[0]);
		int y = Integer.parseInt(coordinates[1]);
		if (!isInBoundary(x, y)) {
			System.out.println(who + " not in boundary");
			return false;
		}

		if (Math.abs(this.x - x) > 1 || Math.abs(this.y - y) > 1) {
			System.out.println(who + " took more than one step");
			return false;
		}
		return true;
	}

	/**
	 * The mouse move. Just a random move to a nearby square 
	 */
	public String clientMove(int currX, int currY) {
//		long time = System.currentTimeMillis();
//		while(System.currentTimeMillis() - time < 20000){
//			
//		}
		Random r = new Random();
		int mx = r.nextInt(3) - 1;
		int my = r.nextInt(3) - 1;
		if(!isInBoundary(currX + mx, currY + my)){
			return clientMove(currX, currY);
		}
//		x = currX+mx;
//		y = currY+my;
		return "MOVE:" + (currX + mx) + "," + (currY + my);
	}
	
	public String clientMove1(int currX, int currY) {
		int newX = currX, newY = currY;
		if(opponentX>currX && opponentY>currY){
			newX = currX-1;
			newY = currX-1;
		}
		else if(opponentX<=currX && opponentY>currY){
			newX = currX + 1;
			newY = currX - 1;
		}
		else if(opponentX>currX && opponentY<=currY){
			newX = currX -1;
			newY = currY + 1;
		}
		else if(opponentX<=currX && opponentY<currY){
			newX = currX + 1;
			newY = currY + 1;
		}
		if(!isInBoundary(newX, newY)){
			clientMove(currX, currY);
		}
		return "MOVE:" + newX + "," + newY;
	}
	public void setOpponentLoc(int x, int y){
		this.opponentX = x;
		this.opponentY = y;
	}

}
